//
//  CollectionViewController.m
//  片刻
//
//  Created by lanou on 16/4/27.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#import "CollectionViewController.h"
#import "BaseTableViewCell.h"
#import "TableViewCellFactory.h"

@interface CollectionViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource; // 存放所有model

@property(nonatomic, strong) NSMutableArray *deleteArr;//删除数据的数组
@property(nonatomic, strong) UIButton *selectAllBtn;//选择按钮
@property(nonatomic, strong) UIView *baseView;//背景view
@property(nonatomic, strong) UIButton *deleteBtn;//删除
@end

@implementation CollectionViewController
- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    //修改导航条标题颜色
    self.navigationItem.title = @"我的收藏";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor grayColor],NSForegroundColorAttributeName,nil]];
}
- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
}
#if 0
- (void)viewDidLoad {
    [super viewDidLoad];
    [self crateBack:@"返回"];
    self.tableView.editing = NO; // 默认tableview的editing 是不开启的
    
    [self createRightItems];
//    [self createRightItem];
    // 取出所有收藏数据
    self.dataSource = [[QHFMDB sharedManager] selectReadCollection];
    self.deleteArr = [NSMutableArray array];
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"ReadDetailListModelCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([ReadDetailModel class])];
}
#pragma mark - 创建全选和多选 删除按钮 实现多选删除cell 方法一
- (void)createRightItems {
    // 选择按钮
    UIButton *selectedBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    selectedBtn.frame = CGRectMake(0, 0, 60, 30);
    [selectedBtn setTitle:@"选择" forState:UIControlStateNormal];
    [selectedBtn addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *selectItem = [[UIBarButtonItem alloc] initWithCustomView:selectedBtn];
    // 全选
    _selectAllBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _selectAllBtn.frame = CGRectMake(0, 0, 60, 30);
    [_selectAllBtn setTitle:@"全选" forState:UIControlStateNormal];
    [_selectAllBtn addTarget:self action:@selector(selectAllBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *selectAllItem = [[UIBarButtonItem alloc] initWithCustomView:_selectAllBtn];
    
    self.navigationItem.rightBarButtonItems = @[selectItem, selectAllItem];
    _selectAllBtn.hidden = YES;
    _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 30, ScreenWidth, 30)];
    _baseView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_baseView];
    //删除按钮
    _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBtn.backgroundColor = [UIColor redColor];
    [_deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    _deleteBtn.frame = CGRectMake(0, 0, ScreenWidth, 30);
    [_deleteBtn addTarget:self action:@selector(deleteClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:_deleteBtn];
    _baseView.hidden = YES;
}
//选择按钮点击响应事件
- (void)selectedBtn:(UIButton *)sender {
    _deleteBtn.enabled = YES;
    //支持同时选中多行
    self.tableView.allowsMultipleSelectionDuringEditing = YES;
    self.tableView.editing = !self.tableView.editing;
    if (self.tableView.editing) {
        _selectAllBtn.hidden = NO;
        _baseView.hidden = NO;
        [sender setTitle:@"完成" forState:UIControlStateNormal];
        
    }else{
        _baseView.hidden = YES;
        _selectAllBtn.hidden = YES;
        [self.deleteArr removeAllObjects]; // 点击完成的时候 将deleteArr数组元素移除
        [sender setTitle:@"选择" forState:UIControlStateNormal];
    }
}
// 全选
- (void)selectAllBtnClick:(UIButton *)sender {
    for (int i = 0; i < self.dataSource.count; i ++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
        [self.deleteArr addObjectsFromArray:self.dataSource];
    }
}
//删除按钮点击事件
- (void)deleteClick:(UIButton *)button {
    if (self.tableView.editing) {
        // 删除数据库中的收藏数据
        for (ReadDetailModel *mode in self.deleteArr) {
            [[QHFMDB sharedManager] deleteReadCollectionWithModel:mode];
        }
        // 删除数组中多选的数组
        [self.dataSource removeObjectsInArray:self.deleteArr];
        [self.deleteArr removeAllObjects];
        [self.tableView reloadData];
    }
    else return;
}
#pragma mark - 创建一个item 用于删除所有收藏 一键清空收藏 方法二
- (void)createRightItem {
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteArray)];
    right.tintColor = [UIColor blackColor];
    self.navigationItem.rightBarButtonItem = right;
}
- (void)deleteArray {
    BOOL result = [[QHFMDB sharedManager] deleteAllReadCollection];
    if (result) {
        // 如果返回yes 说明数据库删除成功 我们清空数组 并刷新界面
        [self.dataSource removeAllObjects];
        // 刷新指定分区 并附加动画
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationLeft];
    }
}
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ReadDetailModel *mode = self.dataSource[indexPath.row];
    BaseTableViewCell *cell = [TableViewCellFactory cellWithModelClass:[mode class] tableView:tableView index:indexPath];
    [cell setDataWithModel:mode];
    return cell;
}
// 打开可编辑模式
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
/*
//选择编辑的方式,按照选择的方式对表进行处理
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ReadDetailModel *mode = self.dataSource[indexPath.row];  // 从数组中找到对应model
        [[QHFMDB sharedManager] deleteReadCollectionWithModel:mode]; // 使用FMDB 将该model删除
        [self.dataSource removeObject:mode];    // 数组也要删除该数据
//        [self.tableView reloadData];
//        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
        // 刷新界面
    }
}
*/
//选择你要对表进行处理的方式  默认是删除方式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
//选中时将选中行的在self.dataSource 中的数据添加到删除数组self.deleteArr中
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ReadDetailModel *model = self.dataSource[indexPath.row];
    // 判断当前是否是编辑状态  编辑状态点击cell 不跳转
    if (self.tableView.editing) {
        [self.deleteArr addObject:model];
    } else {
        ReadInfoViewController *infoVC = [[ReadInfoViewController alloc] initWithNibName:@"ReadInfoViewController" bundle:nil];
        infoVC.contentid = model.contentid;
        infoVC.readModel = model;
        [self.navigationController pushViewController:infoVC animated:YES];
    }
}
//取消选中时 将存放在self.deleteArr中的数据移除
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath  {
    
    [self.deleteArr removeObject:[self.dataSource objectAtIndex:indexPath.row]];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}
// push到详情页面之后 在取消收藏该文章 则 pop回来的时候 将数组重新赋值 用数据库中的数据给数组
- (void)viewWillAppear:(BOOL)animated {
    // 取出所有收藏数据
    self.dataSource = [[QHFMDB sharedManager] selectReadCollection];
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#endif
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
